/**
 * The smallest editable unit (a paragraph on plain text documents).
 * Has an imutable unique id (AtomId).  A lock is required to edit an Atom to prevent
 * 2 or more editors modify the same Atom.
 */
package abstractions;

import emu.EMU;

/**
 * @author rdru
 *
 */
public class Atom {
	AtomId id;					// AtomId
	int lock;					// user the has the lock for this atom (null if none).
	StringBuilder at;			// contents of the atom (text)
	//ArrayList<Integer> al;		// list of line breaks for a window with w width.
	//int w; 					// width for which al was calculated


	// ****************** Constructors:
	// Returns a new atom locked for uid (unlocked if 0) with AtomId based on prev
	// and next.
	// Initialize at with String s when provided.
	Atom(AtomId id, int uid, String s) {
		this.id = id;
		lock = uid;
		at = new StringBuilder(s);
		//EMU.cd.id2atom.put(id, this);	
	}
	
	Atom(int uid, AtomId prev) {
		lock = uid;
		id = new AtomId(prev);
	}
	
	Atom(int uid, AtomId prev, String s) {
		new Atom(new AtomId(prev), uid, s);
	}
	
	Atom(int uid, AtomId prev, AtomId next) {}
	
	Atom(int uid, AtomId prev, AtomId next, String s) {}


	// ****************** other methods
	// Insert character c at position p.
	public void insertC(Pos p, char c) {
		at.insert(p.c, c);		
	}

	// Insert String s at position p.
	public void insertS(Pos p, String s) {}

	// returns the AtomId of the current atom.
	public AtomId getAtomId() {
		return id;
	}

	// returns the int of the lock holder
	public int getlock() {
		return lock;
	}

	// retunrs the number of lines needed to layout the current atom in a cols wide window.
	// Obs: Eh necessario pois quando ao se mudar o conceito de atomo, nao perderemos a
	// modularidade da janela.
	public int getLines(int cols) {
		return 0;		// to compile ...
	}

	// change the windows width and recalculate the line breaks for the current atom
	public void setWidth(int w) {}

	// redraw the editing window ???
	public int redraw(int w) {
		return 0;		// to compile ...
	}
	
	public String getString(){
		return at.toString();
	}
	
	public void setString(String txt){
		at = new StringBuilder(txt);
		return;
	}
}
